/*
 * @Descripttion: 
 * @version: 
 * @Author: wangzhilei
 * @Date: 2021-09-20 03:34:25
 * @LastEditors: wangzhilei
 * @LastEditTime: 2021-09-20 07:07:16
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>

const int SERV_PORT=6000;
const int MAXLINE=2048;

void dg_echo(int sockfd,struct sockaddr *pcliaddr,socklen_t clilen)
{
    int n;
    socklen_t len;
    char mesg[MAXLINE];
    for (;;)
    {
        len=clilen;
        if ((n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len))<0)
        {
            perror("recvfrom error");
            exit(1);
        }
         fputs(mesg, stdout);
        if((n=sendto(sockfd,mesg,n,0,pcliaddr,len))<0)
        {
            perror("sendto error");
            exit(1);
        }
    }
    
}
int main(int argc,char **argv)
{
    int sockfd;
    struct  sockaddr_in servaddr,cliaddr;

     bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = INADDR_ANY;
    servaddr.sin_port =htons(SERV_PORT);
    if ((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        printf("SOCKET ERROR");
        exit(1);
    }
    if (bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)))
    {
        perror("bind error");
        exit(1);
    }
    dg_echo(sockfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
}